package fishBids.group3.behaviour.bidder;

import es.upm.agents.ontology.ListProduct;
import es.upm.agents.ontology.Product;
import es.upm.agents.ontology.ToRegister;
import fishBids.group3.common.Constants;
import fishBids.group3.utils.Utils;
import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.onto.Ontology;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.leap.Collection;

import java.util.Iterator;

public class RegisterBidderBehaviour extends SimpleBehaviour {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2367927292628115358L;
	boolean end = false;
	
	public RegisterBidderBehaviour(Codec codec, Ontology ontology, AID auctioneerID, Agent creatorAgent){
		this.codec = codec;
		this.ontology = ontology;
		this.auctioneerID = auctioneerID;
		this.creatorAgent = creatorAgent;
	}

	public void action()

	{
		Utils.sendMessage(ACLMessage.REQUEST, codec, ontology, auctioneerID, Constants.REGISTER, myAgent);

		// Waits for the arrival of an answer
//		System.out.println("Bidder3>>> Sending Register request. Waiting for an INFORM or REFUSE message");
		// Receive the message 
		MessageTemplate mt = MessageTemplate.and(MessageTemplate.MatchProtocol(Constants.REGISTER), 
				MessageTemplate.and( 
					    MessageTemplate.MatchLanguage(codec.getName()), 
					    MessageTemplate.MatchOntology(ontology.getName()) ));
		
		ACLMessage msg = creatorAgent.blockingReceive(mt); 
		 
		try { 
		  ContentElement ce = null; 
		  if (msg.getPerformative() == ACLMessage.INFORM) { 
			  //System.out.println("Bidder>>> Register INFORM message arrived");
			  ce = creatorAgent.getContentManager().extractContent(msg);
			  if( ce instanceof Action ){
				  Action ac = (Action)ce;
				  Concept cpt = ac.getAction();
				  //System.out.println("instance: " + cpt.getClass().getCanonicalName());
				  if( cpt instanceof ToRegister ){
					  ListProduct listProduct =  ((ToRegister)cpt).getProductList();
					  Collection productsList = listProduct.getProducts();
					  Iterator productsIt = productsList.iterator();
//					  System.out.println("Bidder3>>> Register protocol, received a list of products with: ");
					  while( productsIt.hasNext() ){
						  Product product = (Product) productsIt.next();
//						  System.out.println("\n\ttype: " 
//								  + product.getProductType().getType() + " - amount: " + product.getAmount() );
					  }
					  
					  end = true;
				  }
			  }
//			  return;
		  }
		  if (msg.getPerformative() == ACLMessage.REFUSE) { 
//			  System.out.println("Bidder3>>>>> " + creatorAgent.getName() + " got a REFUSE in Register");
			  end=true;
			  return;
		  }
		  if (msg.getPerformative() == ACLMessage.NOT_UNDERSTOOD) { 
//			  System.out.println("Bidder3>>>>> " + creatorAgent.getName() + " got a NOT_UNDERSTOOD in Register, check!!!!! :(");
			  end=true;
			  return;
		  }
		}catch (Exception e) {
			e.printStackTrace();
		}

	}



	public boolean done ()

	{

		return end;

	}
	
	Ontology ontology;
	Codec codec;
	AID auctioneerID;
	Agent creatorAgent;

}
